<?php
namespace app\admin\controller;

use app\common\controller\Admin ; 

class Equipment extends Admin
{
    


    //控制器初始化
    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Equipment');
    }
    /**
     * 管理列表 
     */
    public function index()
    {
        $map = array();

        $searchFieldList = [
            'equipment_name' => '设备名称',
            'equipment_code' => '设备代码',
            'equipment_address' => '设备安装地点',
        ];
        $searchField = input('searchField/s','');
        $searchValue = input('searchValue/s','');
        if($searchField &&  $searchValue){ $map[$searchField] =['like' , '%' . $searchValue . '%'] ;  }


        $list = $this->model->where($map)->paginate(20,false,['query' => request()->param()]);

        $data = array(
            'list' => $list,

            'searchField' => $searchField,
            'searchValue' => $searchValue,
            'searchFieldList' => $searchFieldList,



            );
        $this->assign($data);

        $this->setMeta('系统管理|设备管理');
        return view();
    }






    /**
     * 添加 or  修改
     * @return [type]           [description]
     */
    public function edit(){

        if(IS_AJAX){
            $data   = $this->request->post();

            $allowField = true;
            $scene = false;
            if ($data) {
                $result = $this->model
                    ->isUpdate(!!$data['id'])
                    ->validate($scene)
                    ->allowField($allowField )
                    ->save($data);

                if (false === $result) {
                    return $this->error( $this->model->getError() );
                } else {
                    // echo $this->model->getLastSql();
                    return $this->success('更新成功');
                }
            } else {
                return $this->error( '提交数据为空！！！' );
            }  
        }else{

            $id = input('id/d' , 0 );
            $dept_id = input('dept_id/d' , -1 );
            // $dopost = input('dopost/s' , '' );
            $info = $this->model-> get($id); //读取数据 



            $this->assign('info' , $info );
            $this->setMeta('系统管理|设备管理');
            return view();
        }


        
    }


    /**
     * 删除配置  一个删除 和 多个删除
     *  
     */
    public function del() {

        return $this->error('不可删除，禁用！！！');


        $id = array_unique((array) input('id/a', 0));

        if (empty($id)) {
            return $this->error('请选择要操作的数据!');
        }

        $map = array('user_id' => array('in', $id));

        // $del_user_role = db('user_role')->where( $map2 )->delete(); //用户表是假删除  不删除关系表

        if ( false === $this->model->where($map)->setField('equipment_state',-1) ) {
            return $this->error('删除失败！' );    // $msg = '', $url = null, $data = '', $wait = 3, array $header = []
        } else {
            return $this->success('删除成功！' );   //$msg = '', $url = null, $data = '', $wait = 3, array $header = []
        }
    }







    public function rule(){

        return '待开发。。。';

    }







}//end class
